package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.text.SimpleDateFormat;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @RequestMapping("/submit")
    public Result submit(@RequestBody Map<String,String> map){
        log.debug("OrderController: submit: map ===> " + map);
        String telephone = map.get("telephone");
        String validateCode = map.get("validateCode");
        String validateRedis = jedisPool.getResource().get(RedisMessageConstant.SENDTYPE_ORDER + "-" + telephone);
        if (validateRedis==null && validateRedis.length() == 0){
            return new Result(false, "验证码失效");
        }
        if (!validateRedis.equals(validateCode)){
            return new Result(false,"验证码输入有误");
        }
        map.put("orderType", Order.ORDERTYPE_WEIXIN);
        Result result = orderService.addOrder(map);
        log.debug("提交预约信息成功");
        return result;
    }

    @RequestMapping("/findOrderById")
    public Result findOrderById(Integer id){
        log.debug("OrderController: findOrderById: id ===> " + id);
        Map<String,Object> map = orderService.findOrderById(id);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String orderDate = simpleDateFormat.format(map.get("orderDate"));
        map.put("orderDate",orderDate);
        log.debug("OrderController: findOrderById: map ===> " + map);
        log.debug("提交预约信息跳转页面回显成功");
        return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,map);
    }

}
